package com.googlecode.layout4j.domain;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Simple implementation.
 * @author Matt Humphreys
 */
/* TODO: equals + hashcode */
public class SimpleContentRefImpl implements SimpleContentRef {
	private static final int HASHCODE_INITIAL_ODD = 77;
	private static final int HASHCODE_MULTIPLIER_ODD = 41;
	
	private String dataId;
	private String dataTypeId;
	private String styleId;
	
	public String getDataId() {
		return dataId;
	}
	public void setDataId(String dataId) {
		this.dataId = dataId;
	}
	
	public String getDataTypeId() {
		return dataTypeId;
	}
	public void setDataTypeId(String dataTypeId) {
		this.dataTypeId = dataTypeId;
	}
	
	public String getStyleId() {
		return styleId;
	}
	public void setStyleId(String styleId) {
		this.styleId = styleId;
	}
	
	public static String cmsAttrs(SimpleContentRef contentRef) {
		StringBuilder str = new StringBuilder();
		str.append("cms-type='content' cms-data-id='" + contentRef.getDataId());
		str.append("' cms-data-type='" + contentRef.getDataTypeId());
		str.append("' cms-style='" + contentRef.getStyleId());
		str.append("'");
		return str.toString();
	}
	
	@Override
	public boolean equals(Object obj) {
		if ((obj != null) && (obj instanceof SimpleContentRef)) {
			SimpleContentRef ref = (SimpleContentRef) obj;
			return new EqualsBuilder()
					.append(ref.getDataId(), dataId)
					.append(ref.getDataTypeId(), dataTypeId)
					.append(ref.getStyleId(), styleId)
					.isEquals();
		} else {
			return false;
		}
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder(HASHCODE_INITIAL_ODD, HASHCODE_MULTIPLIER_ODD)
				.append(dataId)
				.append(dataTypeId)
				.append(styleId)
				.toHashCode();
	}
	
	@Override
	public String toString() {
		// Manual toString (rather than using ToStringBuilder)
		StringBuilder str = new StringBuilder();
		str.append("[dataId=" + dataId);
		str.append(", dataTypeId=" + dataTypeId);
		str.append(", styleId=" + styleId);
		str.append(']');
		return str.toString();
	}
	
	/**
	 * Parameter order is more explicit than an "anonymous" constructor.
	 * @return a new instance
	 */
	public static SimpleContentRef constructByIdTypeStyle(String id, String type, String style) {
		SimpleContentRefImpl ref = new SimpleContentRefImpl();
		ref.setDataId(id);
		ref.setDataTypeId(type);
		ref.setStyleId(style);
		return ref;
	}
}
